package com.sup.generator.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sup.generator.beans.GrUserBean;
import com.sup.generator.config.GrRedisPrefixConfig;
import com.sup.generator.entity.GrMenuEntity;
import com.sup.generator.entity.GrUserEntity;
import com.sup.generator.exception.GrException;
import com.sup.generator.mapper.GrUserMapper;
import com.sup.generator.fixedvalue.GrMessage;
import com.sup.generator.service.GrMenuService;
import com.sup.generator.service.GrRedisService;
import com.sup.generator.service.GrUserService;
import com.sup.generator.utils.GrBase64Util;
import com.sup.generator.utils.GrMd5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Zh
 * @version v1.0
 * @description:
 * @date 2020/11/13 11:40
 */
@Service("grUserServiceImp")
public class GrUserServiceImp extends ServiceImpl<GrUserMapper, GrUserEntity> implements GrUserService {

    @Autowired
    private GrMenuService grMenuService;
    @Autowired
    private GrRedisService redisService;

    /**
     * 登录
     * @param name
     * @param password
     * @return
     */
    @Override
    public GrUserBean login(String name, String password) {
        GrUserEntity userEntity = getByName(name);
        if(userEntity == null){
            throw new GrException(GrMessage.ACCOUNT_DOES_NOT_EXIST);
        }
        password = GrMd5Utils.strToMd5Str(password);
        String databasePassword = userEntity.getPassword();
        if(!password.equals(databasePassword)){
            throw new GrException(GrMessage.ACCOUNT_PASSWORD_INCORRECT);
        }
        Long userId = userEntity.getUId();
        List<GrMenuEntity> grMenu = grMenuService.getByUserId(userId);
        GrUserBean grUserBean = new GrUserBean();
        grUserBean.setGrUserEntity(userEntity);
        grUserBean.setMenuEntityList(grMenu);
        redisService.setString(userId, GrRedisPrefixConfig.LOGIN_TOKEN,grUserBean);
        return grUserBean;
    }

    /**
     * 注册
     * @param grUserEntity
     * @return
     */
    @Override
    public GrUserEntity register(GrUserEntity grUserEntity) {
        String password = GrMd5Utils.strToMd5Str(grUserEntity.getPassword());
        grUserEntity.setPassword(password);
        boolean saveStatus = save(grUserEntity);
        if(!saveStatus){
            throw new GrException(GrMessage.REGISTER_FAIL);
        }
        return grUserEntity;
    }

    /**
     * 根据名字获取
     * @param name
     * @return
     */
    @Override
    public GrUserEntity getByName(String name){
        GrUserEntity grUserEntity = GrUserEntity.getGrUserEntity();
        grUserEntity.setName(name);
        QueryWrapper<GrUserEntity> wrapper = new QueryWrapper<>(grUserEntity);
        return getOne(wrapper);
    }

    /**
     * 登出
     */
    @Override
    public Boolean loginOut(Long userId) {
       return redisService.remove(userId,GrRedisPrefixConfig.LOGIN_TOKEN);
    }

}
